const back = wx.getBackgroundAudioManager();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    avStatus: 0,
    audio: '',
    video: '',
    titles:'',
    bg:'',
  },
  backmusic: function () {
    let that = this
    console.log(that.data.audio);
    console.log(that.data);
    player();
    function player() {
      back.title = that.data.titles;   // 必须要有一个title
      back.src = 'https://wwz.jingyi.icu/'+that.data.audio;
      back.coverImgUrl = 'https://wwz.jingyi.icu/'+that.data.bg;
      back.onEnded(() => {
        player();  // 音乐循环播放
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.setNavigationBarTitle({
      title: '语音讲解',
    })
    this.ibeacons()

  },
  ibeacons() {
    let that = this
    let arrs = []
    wx.request({
      url: 'https://wwz.jingyi.icu/app/Explain/index',
      data: {
        scenic_id: 3
      },
      method: 'POST',
      success: (res) => {
        console.log(res.data.data);
        arrs = res.data.data
        if (res.data.data.length > 0) {
          let uuids = []
          let sendata = []
          for (let item of res.data.data) {
            if (item.deviceid.length >= 36) {
              uuids.push(item.deviceid)
              sendata.push(item)
            }
          }
          console.log(uuids);
          if (uuids.length) {
            console.log(uuids);
            wx.openBluetoothAdapter({
              success: () => {
                //搜索iBeacon设备
                wx.startBeaconDiscovery({
                  uuids: uuids, //多个uuid数组
                  success: function (res) {
                    wx.onBeaconUpdate(function () {
                      //设置监听事件
                      wx.getBeacons({
                        //在监听事件中获取数据
                        success: res => {
                          console.log(res)  //res.beacons 为搜索到的iBeacon数据数组
                          //业务代码
                          let senid = ''
                          res.beacons.find((item) => {
                            for (let i = 0; i < arrs.length; i++) {
                              if (item.uuid.toLocaleUpperCase() == arrs[i].deviceid) {
                                console.log(item);
                                let isms = arrs[i].img.split(',')
                                if (arrs[i].audio != '') {
                                  that.setData({
                                    avStatus: 1,
                                    audio: arrs[i].audio,
                                    titles:arrs[i].title,
                                    bg:arrs[i].img
                                  })
                                  that.backmusic()
                                } else if (arrs[i].video != '') {
                                  that.setData({
                                    avStatus: 2,
                                    video: arrs[i].video,
                                    titles:arrs[i].title,
                                    bg:arrs[i].img
                                  })
                                } else {
                                  that.setData({
                                    avStatus: 0
                                  })
                                }
                                that.setData({
                                  senInfo: arrs[i],
                                  iamges: isms
                                })
                                // 找到要搜索的设备后，及时停止扫描
                                wx.stopBeaconDiscovery();
                                wx.stopPullDownRefresh();//停止刷新
                              }
                            }
                          })
                          console.log(that.data.senInfo);
                          // if(res.beacons){

                          // }
                        }
                      })
                    })
                  },
                  fail: (res) => {
                    console.log('无匹配数据');
                    console.log(res);
                  }
                })
              },
              fail: () => {
                wx.showModal({
                  title: '提醒',
                  content: '请先打开您的手机蓝牙，打开后下拉刷新',
                })
                console.log(uuids);
              }
            })
          }

        } else {
          wx.showModal({
            title: '提醒',
            content: '未搜寻到匹配设备',
          })
        }

      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})